package com.weetop.cfw.base.presenter.imp

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils
import com.weetop.cfw.base.presenter.UpdatePasswordPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.UpdatePasswordView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.VerificationCodeBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.register.UpdatePasswordActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class UpdatePasswordPresenterImp(var updatePasswordView: UpdatePasswordView? = null): UpdatePasswordPresenter {
    /**
     * 重置密码
     * @param mobile 手机号
     * @param userpwd 新密码
     * @param smscode 验证码
     */
    override fun resetPassword(
        updatePasswordActivity: UpdatePasswordActivity,
        mobile: String,
        userpwd: String,
        makeSureUserpwd: String,
        smscode: String
    ) {
        if(mobile.isEmpty()){
            updatePasswordView?.showNativeShortToast("请输入手机号")
        }else if(!RegexUtils.isMobileExact(mobile)){
            updatePasswordView?.showNativeShortToast("请输入合法手机号")
        }else if(smscode.isEmpty()){
            updatePasswordView?.showNativeShortToast("请输入验证码")
        }else if(userpwd.isEmpty()){
            updatePasswordView?.showNativeShortToast("请输入新密码")
        }else if(makeSureUserpwd.isEmpty()){
            updatePasswordView?.showNativeShortToast("请确认密码")
        }else if(userpwd != makeSureUserpwd){
            updatePasswordView?.showNativeShortToast("两次输入密码不一致")
        }else{
            updatePasswordActivity.showLoadingDialog(updatePasswordActivity,"正在找回密码...")
            RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().forgetPassword(mobile, userpwd, smscode),updatePasswordActivity,
                object : RxJavaCallBack<ErrorBean>() {
                    override fun onSuccess(t: ErrorBean?) {
                        updatePasswordView?.resetPasswordSuccess(t)
                        updatePasswordActivity.dismissLoadingDialog()
                    }

                    override fun onRequestError(t: ErrorBean?) {
                        super.onRequestError(t)
                        updatePasswordActivity.dismissLoadingDialog()
                    }

                    override fun onErrorResponse(throwable: Throwable?) {
                        super.onErrorResponse(throwable)
                        updatePasswordActivity.dismissLoadingDialog()
                    }

                })
        }
    }

    /**
     * 获取验证码
     * @param mobile 手机号
     */
    override fun getVerificationCode(
        updatePasswordActivity: UpdatePasswordActivity,
        mobile: String
    ) {
        when {
            mobile.isEmpty() -> {
                updatePasswordView?.showNativeShortToast("请输入手机号")
            }
            !RegexUtils.isMobileExact(mobile) -> {
                LogUtils.d("mobile",mobile)
                updatePasswordView?.showNativeShortToast("请输入合法手机号")
            }
            else -> {
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getVerificationCode(mobile),updatePasswordActivity,object:
                    RxJavaCallBack<VerificationCodeBean>(){
                    override fun onSuccess(t: VerificationCodeBean?) {
                        LogUtils.d(t?.toString())
                        updatePasswordView?.getVerificationCodeSuccess(t)
                    }
                })
            }
        }

    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        updatePasswordView = baseView as? UpdatePasswordView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        updatePasswordView = null
    }
}